//
//  SNSleepDetailVc.m
//  project
//
//  Created by 黄建华 on 2017/12/5.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNSleepDetailVc.h"
#import "SNSleepDetailView.h"
#import "SNSleepChartVc.h"

@interface SNSleepDetailVc () <UIScrollViewDelegate>
@property (nonatomic, strong) UILabel           *currentDateLabel;
@property (nonatomic, strong) SNSleepDetailView *sleepDetailView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *otherView;

@end

@implementation SNSleepDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *stepDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    stepDetailButton.frame = SNFRAME(5, 30 + FitSimpleIpx(0, 20), 50, 50);
    [stepDetailButton setImageNormal:@"进入"];
    stepDetailButton.center = CGPointMake(25, self.leftButton.centerY );
    [stepDetailButton addTarget:self
                         action:@selector(backView)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stepDetailButton];
    
    UIButton *sleepChartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sleepChartButton.frame = CGRectMake(screenWidth - 44 -5, 33 + FitSimpleIpx(0, 20), 44, 44);
    [sleepChartButton setImageNormal:@"数据"];
    sleepChartButton.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 4, 2);
    sleepChartButton.center = CGPointMake(screenWidth - 22 -10, self.leftButton.centerY);
    [sleepChartButton addTarget:self
                         action:@selector(showSleepChartVc)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sleepChartButton];
    
    _currentDateLabel               = [[UILabel alloc] initWithFrame:SNFRAME(0, 64, HHHWIDTH, 25)];
    _currentDateLabel.textColor     = SNCOLORBLACK;
    _currentDateLabel.textAlignment = NSTextAlignmentCenter;
    _currentDateLabel.font          = SNFONT(20.0);
    _currentDateLabel.center        = CGPointMake(screenWidth/2, self.leftButton.centerY);
    _currentDateLabel.text          = [NSString stringWithFormat:@"%@%@",SN_Text(@"今天"), SN_Text(@"睡眠")];
    [self.view addSubview:_currentDateLabel];
    
////    SNShadowView *shadowLine = [[SNShadowView alloc] init];
////    shadowLine.frame = SNFRAME(0,20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
////    [shadowLine showWithType:1];
////    [self.view addSubview:shadowLine];
//    UIImageView *shadowLine = [[UIImageView alloc] init];
//    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
//    shadowLine.image = [UIImage image:@"lineImage"];
//    [self.view addSubview:shadowLine];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showSleepInfo) name:@"showSleepInfo" object:nil];
    
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = SNFRAME(0, _currentDateLabel.bottom, HHHWIDTH, HHHHEIGHT - 64 + 20);
//    _scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_scrollView];
    
    CGFloat heightSize = HHHHEIGHT - 64 - 60;
    
    _sleepDetailView       = [[SNSleepDetailView alloc] initWithFrame:SNFRAME(0, 20, HHHWIDTH, HHHHEIGHT - 64 - 44)];
    [_scrollView addSubview:_sleepDetailView];
    [_sleepDetailView updateView];
    
//    _otherView = [[UIView alloc] init];
//    _otherView.frame = SNFRAME(0, _sleepDetailView.bottom - 50, HHHWIDTH, 200);
//    [_scrollView addSubview:_otherView];
//    
//    _scrollView.contentSize = CGSizeMake(HHHWIDTH, heightSize + _otherView.height);
//    _otherView.backgroundColor = [UIColor redColor];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSleepInfo" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    _currentDateLabel.text = [NSString stringWithFormat:@"%@ %@", SN_Text(@"今天"), SN_Text(@"睡眠")];
    if (_sleepDetailView) {
        [_sleepDetailView removeFromSuperview];
        _sleepDetailView = nil;
    }
    _sleepDetailView       = [[SNSleepDetailView alloc] initWithFrame:SNFRAME(0, 20, HHHWIDTH, HHHHEIGHT - 64 - 44)];
    [_scrollView addSubview:_sleepDetailView];
    [_sleepDetailView updateView];
    CustomTabBar         *tabBar   = (CustomTabBar *)self.tabBarController;
    [tabBar hiddenTheTabBarView];
}

- (void)showSleepChartVc {
    SNSleepChartVc *vc = [[SNSleepChartVc alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)updateView:(KKBLEAcceptType)type {
    if (type == KKBLEAcceptTypeDataSleep) {
        _currentDateLabel.text = [NSString stringWithFormat:@"%@ %@",SN_Text(@"今天"), SN_Text(@"睡眠")];
        if (_sleepDetailView) {
            [_sleepDetailView removeFromSuperview];
            _sleepDetailView = nil;
        }
        _sleepDetailView       = [[SNSleepDetailView alloc] initWithFrame:SNFRAME(0, 20, HHHWIDTH, HHHHEIGHT - 64 - 44)];
        [_scrollView addSubview:_sleepDetailView];
        [_sleepDetailView updateView];
    }
}

// 分享
- (void)shareButtonClick {
    UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
    [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:image];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                        UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                        UIActivityTypeOpenInIBooks]];
    activityViewController.excludedActivityTypes = excludedActivityTypes;
    [self presentViewController:activityViewController animated:YES completion:nil];
    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
    };
}

- (void)backView {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showSleepInfo
{
    HDAlertView *alertView = [HDAlertView alertViewWithTitle:HHH_Text(@"关于睡眠准确度") andMessage:HHH_Text(@"睡眠解释")];
    [alertView addButtonWithTitle:HHH_Text(@"退出") type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
    }];
    [alertView show];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

